package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.web.dao.WebColumnTypeDao;
import ysq.xyg.web.model.po.WebColumnType;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebTagContent;
import ysq.xyg.web.model.vo.WebColumnTypeVo;
import ysq.xyg.web.service.WebColumnTypeService;
import ysq.xyg.web.service.WebTagContentService;
import ysq.xyg.web.util.WebBaseContent;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 网站模块-网站栏目service实现
 *
 * @author shengQiang yu
 * @date 2019-6-5 22:15:56
 */
@Service("webColumnTypeService")
public class WebColumnTypeServiceImpl extends BaseServiceImpl<WebColumnType> implements WebColumnTypeService {
    @Resource
    private WebTagContentService webTagContentService;
    @Resource
    private WebColumnTypeDao webColumnTypeDao;

    private WebColumnType webColumnType;

    @Autowired
    public void setBaseDao(WebColumnTypeDao webColumnTypeDao) {
        this.baseDao = webColumnTypeDao;
    }


    /**
     * 分页查询
     *
     * @param webColumnTypeVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 22:15:56
     */
    public ReturnEntity page(WebColumnTypeVo webColumnTypeVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<WebColumnType> list = webColumnTypeDao.listLeft(webColumnTypeVo);
        PageInfo<WebColumnType> page=new PageInfo<WebColumnType>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webColumnTypeVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 22:15:56
     */
    public ReturnEntity addOrUpdate(WebColumnTypeVo webColumnTypeVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webColumnTypeVo.getId())){
            webColumnType=new WebColumnType(currentUser);
            isUpate=false;
        }else{
            webColumnType=selectByPrimaryKey(webColumnTypeVo.getId());
            if(ObjectUtil.isNull(webColumnType)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webColumnType.setUpdateTime(new Date());
            webColumnType.setUpdateUser(currentUser.getId());
        }
        webColumnType.setName(webColumnTypeVo.getName());
        webColumnType.setColType(webColumnTypeVo.getColType());
        webColumnType.setThumImage(webColumnTypeVo.getThumImage());
        webColumnType.setCoverImage(webColumnTypeVo.getCoverImage());
        webColumnType.setSeoKeyword(webColumnTypeVo.getSeoKeyword());
        webColumnType.setDescription(webColumnTypeVo.getDescription());
        webColumnType.setSort(webColumnTypeVo.getSort());
        webColumnType.setShowType(webColumnTypeVo.getShowType());
        webColumnType.setUserId(currentUser.getId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webColumnType);
        }else{
            num=insert(webColumnType);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }else{
            webTagContentService.delete(new WebTagContent("",webColumnType.getId()));
            if(StrUtil.isNotEmpty(webColumnTypeVo.getTagIds())){
                String[] list=webColumnTypeVo.getTagIds().split(",");
                if(list!=null&&list.length>0){
                    for(String id:list){
                        webTagContentService.insert(new WebTagContent(id,WebBaseContent.web_tag_type_lm,webColumnType.getId(),currentUser));
                    }
                }
            }
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 22:15:56
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebColumnType webColumnType=selectByPrimaryKey(id);
        if(webColumnType!=null){
            webColumnType.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webColumnType.setUpdateTime(new Date());
            webColumnType.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webColumnType);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }
    /**
     * 获取下拉框集合
     *
     * @param code code值
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    public ReturnEntity acceptSelect(String colType,String code) {
        List<WebColumnType> list = acceptList(new WebColumnTypeVo(colType));
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(WebColumnType webColumnType:list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(webColumnType.getId());
            sysDictContent.setName(webColumnType.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(webColumnType.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
    /**
     * 通过标签id获取所属目录
     *
     * @param tagId 标签id
     * @author shengQiang yu
     * @date 2019-04-02 12:42
     */
    public List<WebColumnType> listLeftByTagId(String tagId){
        return webColumnTypeDao.listLeftByTagId(new WebColumnTypeVo(WebBaseContent.web_col_type_ptwz,BaseConstant.base_show_type_zs,tagId));
    }
    /**
     * 获取集合
     *
     * @param webColumnTypeVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-6-5 22:15:56
     */
    public List<WebColumnType> acceptList(WebColumnTypeVo webColumnTypeVo){
        Example example = new Example(WebColumnType.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webColumnTypeVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webColumnTypeVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webColumnTypeVo.getColType())){
            criteria.andEqualTo("colType",webColumnTypeVo.getColType());
        }
        if(StringUtil.isNotEmpty(webColumnTypeVo.getName())){
            criteria.andLike("name","%"+webColumnTypeVo.getName()+"%");
        }
        if(StringUtil.isNotEmpty(webColumnTypeVo.getShowType())){
            criteria.andEqualTo("showType",webColumnTypeVo.getShowType());
        }
        if(StringUtil.isNotEmpty(webColumnTypeVo.getUserId())){
            criteria.andEqualTo("userId",webColumnTypeVo.getUserId());
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return webColumnTypeDao.selectByExample(example);
    }
}
